from bs4 import BeautifulSoup
import csv

with open('./weixin.html') as target_file:
    # print(target_file.read())
    soup = BeautifulSoup(target_file.read())
    div_viewer = soup.find(name='div', attrs={'class': "news-box"})
    # print(div_viewer)
    item_list = div_viewer.find_all(name='li')

    with open('names.csv', 'w', newline='') as csvfile:
        fieldnames = ['club_name', 'club_describe', 'club_verification', 'current_topic']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

        writer.writeheader()
        for item in item_list:
            club_name = item.find(name='p', attrs={'class': "info"}).get_text()
            dd_list = item.find_all(name='dd')
            if len(dd_list) > 0:
                club_describe = dd_list[0].get_text() 
            else:
                club_describe = '-'
            if len(dd_list) > 1:
                club_verification = dd_list[1].get_text()
            else:
                club_verification = '-'
            if len(dd_list) > 2:
                current_topic = dd_list[2].get_text()
            else:
                current_topic = '-'
            writer.writerow({
                'club_name': club_name,
                'club_describe': club_describe,
                'club_verification': club_verification,
                'current_topic': current_topic,
            })
            # writer.writerow({'club_name': 'Lovely', 'club_describe': 'Spam'})
            # writer.writerow({'club_name': 'Wonderful', 'club_describe': 'Spam'})
        
        print(item)
        # re.search(r'', item)

